<template>
  <div class="home-container">
    <el-container>
      <!-- 头部区域 -->
      <el-header>
        <router-link to="/home">
          <img src="../assets/logo_back.png" alt="电商后台管理系统">
          <h3>电商后台管理系统</h3>
        </router-link>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <el-container>
        <!-- 侧边栏区域 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <!-- 控制菜单收缩和展开 -->
          <div class="toggle-button" @click="toggleCollaspse">|||</div>
          <!-- 侧边栏菜单区域 -->
          <el-menu class="el-menu-vertical-demo" background-color="#313743" text-color="#fff" unique-opened router
            :default-active="activePath" :collapse="isCollapse" :collapse-transition="false"
            active-text-color="#2742c3">
            <!-- 一级菜单 -->
            <el-submenu :index="item.id +''" v-for="item in menuList" :key="item.id">
              <!-- 一级菜单的模板 -->
              <template slot="title">
                <i :class=menuIcon[item.id]></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id"
                @click="changeActivePath('/'+subItem.path)">
                <!-- 二级菜单的模板 -->
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧主要内容区域 -->
        <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  // 组件用到的数据
  data() {
    return {
      // 左侧菜单列表
      menuList: [],
      // 左侧菜单图标
      menuIcon: {
        125: 'iconfont icon-users',
        101: 'iconfont icon-tijikongjian',
        103: 'iconfont icon-3702mima',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao',
      },
      // 是否折叠左侧菜单
      isCollapse: false,
      // 当前处于激活状态的菜单索引
      activePath: '',
    }
  },
  created() {
    // 获取菜单列表
    this.getMenuList()
    // 获取当前的菜单索引值
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  // 组件用到的方法
  methods: {
    // 退出登录
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取菜单列表
    async getMenuList() {
      const { data: res } = await this.$http.get('/menus')
      if (res.meta.status != 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
      // console.log(this.menuList)
    },
    // 折叠或展开左侧菜单
    toggleCollaspse() {
      this.isCollapse = !this.isCollapse
    },
    // 改变处于激活状态的菜单索引值
    changeActivePath(activePath) {
      this.activePath = activePath
      // 存入sessionStage
      window.sessionStorage.setItem('activePath', activePath)
      console.log(this.$route)
    },
  },
}
</script>

<style lang="less" scoped>
// 页面最大容器
.home-container,
.el-container {
  color: #fff;
  height: 100%;
  overflow: hidden;
}
// 顶部区域
.el-header {
  height: 60px;
  background-color: #363d40;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  // 顶部左侧区域
  a {
    color: #fff;
    text-decoration: none;
    display: flex;
    align-items: center;
  }
  img {
    width: 30px;
    height: 30px;
    padding-right: 12px;
  }
  h3 {
    display: inline;
  }
  // 顶部右侧区域
  button {
    margin: 5px;
  }
}
// 侧边栏
.el-aside {
  background-color: #313743;
  // 折叠菜单的按钮
  .toggle-button {
    font-size: 14px;
    padding: 6px;
    cursor: pointer;
    text-align: center;
    letter-spacing: 0.3em;
    background-color: #475163;
  }
  // 修改菜单
  .el-menu {
    border-right: 0px;
  }
}
.el-main {
  height: 100%;
  background: #e9edf1;
}

// 设置icon图标
.iconfont {
  margin-right: 7px;
}
</style>
